/*
 ***************************************************************************
 *           Copyright © 2009 by konglinglong                              *
 *           konglinglong@gmail.com                                        *
 *           我的主页:https://sites.google.com/site/konglinglong/           *
 ***************************************************************************
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************
 */


#include "LongEventProcesser.h"
#include "LongDebug.h"
#include "Longdefines.h"
#include "LongMain.h"
#include "EventStruct.h"
#include "PacketBuilder.h"
#include "LongUser.h"
#include "LoginModule.h"
#include "NetworkManager.h"
#include "GuiHandling.h"
#include "PacketStorage.h"
#include "PacketProcesser.h"
#include "Packet.h"


//
LongEventProcesser::LongEventProcesser(QObject *parent)
	:QObject(parent)
{
	connect(this, SIGNAL(longEventHandlingSignal(LONG_EVENT *)), this, SLOT(longEventHandlingSlot(LONG_EVENT *)));
}

LongEventProcesser::~LongEventProcesser()
{
	//打印调试信息
	DWHERE();
}

void LongEventProcesser::longEventHandling(LONG_EVENT *event)
{
	emit longEventHandlingSignal(event);
}





void LongEventProcesser::longEventHandlingSlot(LONG_EVENT *event)
{
	//打印调试信息
	DWHERE();
	DPRINT("- - - > <LONG_EVENT>");
	DPRINT("- - - > eventID:%ld",event->eventID);
	DPRINT("- - - > eventSource:%s",event->source);
	DPRINT("- - - > eventDescription:%s",event->description);
	DPRINT("- - - > eventType:%d",event->type);

	switch(event->type)
	{
		case LONG_EVENT::netInitSucceed:
			processNetInitSucceedEvent(event);
			break;
		case LONG_EVENT::loginRequest:
			processLoginRequestEvent(event);
			break;
		case LONG_EVENT::needVerify:
			processNeedVerifyEvent(event);
			break;
		case LONG_EVENT::needActivated:
			processNeedActivatedEvent(event);
			break;
		case LONG_EVENT::passwdError:
			processPasswdErrorEvent(event);
			break;
		case LONG_EVENT::logoutRequest:
			processLogoutRequestEvent(event);
			break;
		case LONG_EVENT::loginSucceed:
			processLoginSucceedEvent(event);
			break;
		case LONG_EVENT::loginFailed:
			processLoginFailedEvent(event);
			break;
		case LONG_EVENT::fallLine:
			processFallLineEvent(event);
			break;
		case LONG_EVENT::gotAllBuddyList:
			processGotAllBuddyListEvent(event);
			break;
		case LONG_EVENT::gotBuddyOnlineList:
			processGotBuddyOnlineListEvent(event);
			break;
		case LONG_EVENT::gotUserInfo:
			processGotUserInfoEvent(event);
			break;
		case LONG_EVENT::gotUserSig:
			processGotUserSigEvent(event);
			break;
		case LONG_EVENT::newLoginStatus:
			break;
		default:
			;
	}

}



void LongEventProcesser::processLoginRequestEvent(LONG_EVENT *event)
{
	LongIM->netManager->startConnect();
	SAFE_DELETE(event);
}

void LongEventProcesser::processNeedActivatedEvent(LONG_EVENT *event)
{
	LongIM->guiHandling->needActivated();
	SAFE_DELETE(event);
}

void LongEventProcesser::processNeedVerifyEvent(LONG_EVENT *event)
{
	LongIM->guiHandling->needVerify();
	SAFE_DELETE(event);
}

void LongEventProcesser::processPasswdErrorEvent(LONG_EVENT *event)
{
	LongIM->guiHandling->passwdError();
	SAFE_DELETE(event);
}

void LongEventProcesser::processNetInitSucceedEvent(LONG_EVENT *event)
{
	LongIM->longUser->init();
	LongIM->loginModule = new LoginModule();
	LongIM->loginModule->startLogin();
	SAFE_DELETE(event);
}

void LongEventProcesser::processLoginSucceedEvent(LONG_EVENT *event)
{
	LongIM->packetStorage->startKeepAliveTimer();
	LongIM->guiHandling->loginSucceed();

	OutPacket *getFriendList = LongIM->packetBuilder->buildFriendListRequestPacket(NULL);
	LongIM->packetStorage->appendOutPacket(getFriendList);

	SAFE_DELETE(event);
	SAFE_DELETE(LongIM->loginModule);
}

void LongEventProcesser::processLoginFailedEvent(LONG_EVENT *event)
{
	LongIM->packetStorage->stopKeepAliveTimer();
	LongIM->packetStorage->deleteAllPacket();

	//打印调试信息
	DWHERE();

	LongIM->guiHandling->loginFailed();

	SAFE_DELETE(event);
}

void LongEventProcesser::processFallLineEvent(LONG_EVENT *event)
{
	LongIM->packetStorage->stopKeepAliveTimer();
	LongIM->packetStorage->deleteAllPacket();

	LongIM->guiHandling->fallLine();

	SAFE_DELETE(event);
}

void LongEventProcesser::processGotAllBuddyListEvent(LONG_EVENT *event)
{
	OutPacket *getOnlineList = LongIM->packetBuilder->buildFriendOnlineListRequestPacket(NULL);
	LongIM->packetStorage->appendOutPacket(getOnlineList);

	SAFE_DELETE(event);
}

void LongEventProcesser::processGotBuddyOnlineListEvent(LONG_EVENT *event)
{
	uint32 qqNum = Packet::getQQNum();
	OutPacket *getUserInfo = LongIM->packetBuilder->buildGetUserInfoPacket(qqNum);
	LongIM->packetStorage->appendOutPacket(getUserInfo);

	SAFE_DELETE(event);
}

void LongEventProcesser::processGotUserInfoEvent(LONG_EVENT *event)
{
	LongIM->guiHandling->fetchUserInfo();

	OutPacket *getUserSig = LongIM->packetBuilder->buildUserSignatureRequestPacket(LongIM->longUser->signature);
	LongIM->packetStorage->appendOutPacket(getUserSig);

	SAFE_DELETE(event);
}

void LongEventProcesser::processGotUserSigEvent(LONG_EVENT *event)
{

	SAFE_DELETE(event);
}

void LongEventProcesser::processLogoutRequestEvent(LONG_EVENT *event)
{
	LongIM->packetStorage->stopKeepAliveTimer();
	LongIM->packetStorage->deleteAllPacket();

	//直接发送三次
	OutPacket *logoutPacket = LongIM->packetBuilder->buildLogoutRequestPacket();
	LongIM->packetProcesser->processOutPacketSlot(logoutPacket);

	logoutPacket = LongIM->packetBuilder->buildLogoutRequestPacket();
	LongIM->packetProcesser->processOutPacketSlot(logoutPacket);

	logoutPacket = LongIM->packetBuilder->buildLogoutRequestPacket();
	LongIM->packetProcesser->processOutPacketSlot(logoutPacket);

	LongIM->longUser->setCurrentStatus(LongUser::offline);
	LongIM->guiHandling->logoutRequest();

	SAFE_DELETE(event);
}
